package com.aos.eap.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.aos.eap.R;
import com.aos.eap.base.Constant;
import com.aos.eap.bean.EventMessageBean;
import com.aos.eap.bean.LoginBean;
import com.aos.eap.bean.net.BaseResponseBean;
import com.aos.eap.utils.MyUtils;
import com.aos.eap.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseActivity;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.greenrobot.eventbus.EventBus;

public class DialogActivity extends FreeUI_BaseActivity {

    private FreeUI_GeneralFragmentDialog tipDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutResId() {
        return 0;
    }

    @Override
    protected void initView() {
        final String extra = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        if (extra.contains("exchange")) {
            final FreeUI_GeneralFragmentDialog freeUI_generalFragmentDialog = new FreeUI_GeneralFragmentDialog();
            View view = LayoutInflater.from(DialogActivity.this).inflate(R.layout.dialog_exchange_class_verify, null);
            TextView tvCancel = view.findViewById(R.id.tvCancel);
            TextView tvConfirm = view.findViewById(R.id.tvConfirm);
            final FreeUI_ClearEditText etUserId = view.findViewById(R.id.etUserId);
            final FreeUI_ClearEditText etPwd = view.findViewById(R.id.etPwd);
            MyUtils.setEditTextListener(etUserId, this, s -> MyUtils.setEditTextFocus(etPwd));
            tvCancel.setOnClickListener(v -> freeUI_generalFragmentDialog.dismiss());
            tvConfirm.setOnClickListener(v -> {
                if (MyUtils.isFastClick()){
                    showToast("您点击的太快了",false);
                    return;
                }
                String userId = etUserId.getText().toString().trim();
                if (FreeApi_StringUtils.isEmpty(userId)) {
                    showToast("请扫描被交接人账号", false);
                    MyUtils.setEditTextFocus(etUserId);
                    return;
                }
                String pwd = etPwd.getText().toString().trim();
                if (FreeApi_StringUtils.isEmpty(pwd)) {
                    showToast("请扫描被交接人密码", false);
                    MyUtils.setEditTextFocus(etPwd);
                    return;
                }

                if (userId.equals(Constant.CURRENT_USER)){
                    showToast("被交接用户不能是当前登录用户",false);
                    MyUtils.setEditTextFocus(etPwd);
                    return;
                }
                JsonObject map = new JsonObject();
                map.addProperty("fromuserid", Constant.CURRENT_USER);
                map.addProperty("fromuserpwd", Constant.CURRENT_PWD);
                map.addProperty("touserid", userId);
                map.addProperty("touserpwd", pwd);
                NetUtils.requestGetNet(this, "EMS_VailidateShift", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<LoginBean>>(
                        new TypeToken<BaseResponseBean<LoginBean>>() {
                        }.getType(), getSupportFragmentManager(), this) {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<LoginBean>> response) {
                        if (response.body().isSuccess(getApplicationContext(), false,getSupportFragmentManager())) {
                            freeUI_generalFragmentDialog.dismiss();
                            Constant.LAST_USER = Constant.CURRENT_USER;
                            Constant.LAST_PWD = Constant.CURRENT_PWD;
                            Constant.CURRENT_USER = userId;
                            Constant.CURRENT_PWD = pwd;
                            Constant.CURRENT_LOGINBEAN = response.body().getResponse().getBody();
                            EventBus.getDefault().post(new EventMessageBean());
                            showToast("验证成功，当前登录人已切换为"+userId,true);
                            startActivity(new Intent(DialogActivity.this, MFG_ExchangeClassActivity.class));
                        }
                    }
                    @Override
                    public void onError(Response<BaseResponseBean<LoginBean>> response) {
                        super.onError(response);
                        uploadLog(response.getException());
                        loadError(response.getException(), "EMS_VailidateShift");
                    }
                });
            });
            freeUI_generalFragmentDialog.setLayout(view);
            freeUI_generalFragmentDialog.setCallBack(DialogActivity.this::finish);
            freeUI_generalFragmentDialog.show(getSupportFragmentManager(), "eqp_scan_dialog");
        }
    }

    protected void uploadLog(Throwable e){
        JsonObject map = new JsonObject();
        map.addProperty("Content", Log.getStackTraceString(e));
        map.addProperty("MACADDR", Constant.MAC_ADDR);
        NetUtils.requestGetNet(this, "EMS_PostLog", map, new FreeUI_EntityCallBack<BaseResponseBean<?>>(
                new TypeToken<BaseResponseBean<?>>() {
                }.getType()) {
            @Override
            public void onSuccess(Response<BaseResponseBean<?>> response) {
            }

            @Override
            public void onError(Response<BaseResponseBean<?>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_PostLog");
            }
        });
    }


    @Override
    protected void initData() {

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return null;
    }
}
